<script setup>
import { ref, reactive, onMounted } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { Plus } from '@element-plus/icons-vue'
import * as admin from '@/api/admin.js'
import { useTokenStore } from '@/stores/token.js';
const tokenStore = useTokenStore();

// 模拟数据
const records = ref([]);
const currentPage = ref(1);
const pageSize = ref(10);
const total = ref(0);
const addModal = ref(false);
const updateModal = ref(false);
const search_record = ref('')
const studentlist = ref([]);


const updateData = ref({
  id: '',
  studentId: "",
  record: "",
  img: "",
})

const addData = ref({
  studentId: "",
  record: "",
  img: "",
})

const showDialog = (row) => {
  updateData.value.id = row.id;
  updateData.value.studentId = row.studentId;
  updateData.value.record = row.record;
  updateData.value.img = row.img;
}


const fetchRecords = async () => {
  const param_user = {
    currentPage: currentPage.value
  }
  const tearch_data = await admin.findstudentlist(param_user)
  studentlist.value = tearch_data.data;
  console.log(studentlist.value);
  const param = {
    currentPage: currentPage.value,
    record: search_record.value
  }
  const res = await admin.findGrowthRecord(param)
  records.value = res.data;
  total.value = res.total;
};

const searchRecords = () => {
  fetchRecords();
};

const deleteRecord = (id) => {
  const param = {
    id: id
  }
  ElMessageBox.confirm('确认删除该成长记录？', '提示', { type: 'warning' })
    .then(async () => {
      const res = await admin.delGrowthRecord(param)
      if (res.code == 0) {
        fetchRecords();
      }
      ElMessage.success('删除成功');
    })
    .catch(() => ElMessage.info('取消删除'));
};

const handlePageChange = (page) => {
  currentPage.value = page;
  fetchRecords();
};

const addMethod = async() => {
  const res = await admin.addGrowthRecord(addData.value)
  if (res.code == 0) {
    fetchRecords();
  }
  ElMessage.success('增加成功');
  addModal.value = false;
};

const editMethod = async() => {
  const res = await admin.updateGrowthRecord(updateData.value)
  if (res.code == 0) {
    fetchRecords();
  }
  ElMessage.success('修改成功');
  updateModal.value = false;
};


onMounted(() => {
  fetchRecords();
});

const uploadSuccess = (result)=>{
  updateData.value.img = result.data;
  addData.value.img = result.data;
}

</script>

<template>
  <div class="page">
    <el-card class="search-box">
      <el-form :inline="true" class="search-form">
        <el-form-item label="记录内容">
          <el-input v-model="search_record" placeholder="请输入记录内容" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="searchRecords" icon="el-icon-search">搜索</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-button type="success" class="add-button" @click="addModal = true">➕ 添加记录</el-button>

    <el-table :data="records" border>
      <el-table-column prop="yonghu" label="学生" />
      <el-table-column prop="record" label="记录内容" />
      <el-table-column label="记录图片" width="120">
        <template #default="{ row }">
          <el-image :src="row.img" fit="contain" width="60" height="60" />
        </template>
      </el-table-column>
      <el-table-column prop="createdAt" label="记录时间" width="150" />
      <el-table-column label="操作" width="180">
        <template #default="{ row }">
          <el-button type="primary" size="small" @click="showDialog(row),updateModal=true">修改</el-button>
          <el-button type="danger" size="small" @click="deleteRecord(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      :total="total"
      :page-size="pageSize"
      @current-change="handlePageChange"
    />
  </div>


  <el-drawer
    v-model="updateModal"
    title="修改成长记录"
    direction="rtl"
    class="demo-drawer"
    >
    <div class="demo-drawer__content">
    <el-form 
        ref="updateFormRef" 
        :model="updateData" 
        :rules="rules" 
        label-width="80px"
        >
    <el-form-item label="记录内容" prop="record">
      <el-input 
        v-model="updateData.record" 
        type="textarea"
        placeholder="请输入记录内容"
        maxlength="200"
        show-word-limit
        clearable
      />
    </el-form-item>

    <el-form-item label="学生" prop="teacherId">
      <el-select v-model="updateData.studentId" placeholder="请选择学生">
        <el-option v-for="student in studentlist" :key="student.id" :label="student.name" :value="student.id" />
      </el-select>
    </el-form-item>

    <el-form-item label="记录图片" prop="img">
      <el-upload
        class="avatar-uploader"
        action="http://localhost:8080/upload/image"
        name="file"
        :show-file-list="false"
        :on-success="uploadSuccess"
        >
        <img v-if="updateData.img" :src="updateData.img" class="avatar" />
        <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
      </el-upload>
    </el-form-item>

      </el-form>
      <div class="demo-drawer__footer">
        <el-button @click="updateModal=false">取消</el-button>
        <el-button type="primary" @click="editMethod">提交</el-button>
      </div>
    </div>
  </el-drawer>

<el-dialog 
  v-model="addModal" 
  title="添加成长记录" 
  width="40%" 
  center 
  class="custom-dialog"
>
  <el-form 
    ref="addFormRef"
    :model="addData" 
    :rules="rules"
    label-width="100px" 
    style="padding: 10px 20px"  
    size="large"
  >

    <el-form-item label="记录内容" prop="record">
      <el-input 
        v-model="addData.record" 
        type="textarea"
        placeholder="请输入记录内容"
        maxlength="200"
        show-word-limit
        clearable
      />
    </el-form-item>

    <el-form-item label="学生" prop="teacherId">
      <el-select v-model="addData.studentId" placeholder="请选择学生">
        <el-option v-for="student in studentlist" :key="student.id" :label="student.name" :value="student.id" />
      </el-select>
    </el-form-item>

    <el-form-item label="记录图片" prop="img">
      <el-upload
        class="avatar-uploader"
        action="http://localhost:8080/upload/image"
        name="file"
        :show-file-list="false"
        :on-success="uploadSuccess"
        >
        <img v-if="addData.img" :src="addData.img" class="avatar" />
        <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
      </el-upload>
    </el-form-item>
  </el-form>

  <template #footer>
    <span class="dialog-footer">
      <el-button @click="addModal = false">取消</el-button>
      <el-button 
        type="primary" 
        @click="addMethod"
      >
        提交
      </el-button>
    </span>
  </template>
</el-dialog>

</template>

<style scoped>
.page {
  padding: 30px;
  background: #f7f7f7;
}

.search-box {
  background: #fff;
  border-radius: 10px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  padding: 20px;
  margin-bottom: 20px;
}

.add-button {
  background-color: #67c23a;
  color: white;
  border-radius: 8px;
  font-size: 14px;
}

.el-button {
  margin: 0 5px;
}

.el-table {
  margin-top: 20px;
}

.el-pagination {
  margin-top: 20px;
  text-align: center;
}
</style>
